package com.utils;


import java.security.NoSuchAlgorithmException;

import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;

//import org.apache.tomcat.util.codec.binary.Base64;
import org.apache.commons.codec.binary.Base64;



public class DES {
	    private static String key="mini.com";
	    private static String transformation="DES";
	    private static String algorithm="DES";
	    public static String encryptDES(String input) throws Exception {
	    	
	        // 获取加密对象
	        Cipher cipher = Cipher.getInstance(transformation);
	        // 创建加密规则
	        // 第一个参数key的字节
	        // 第二个参数表示加密算法
	        SecretKeySpec sks = new SecretKeySpec(key.getBytes(), algorithm);
	        // ENCRYPT_MODE：加密模式
	        // DECRYPT_MODE: 解密模式
	        // 初始化加密模式和算法
	        cipher.init(Cipher.ENCRYPT_MODE,sks);
	        // 加密
	        byte[] bytes = cipher.doFinal(input.getBytes());

	        // 输出加密后的数据
	        String encode = Base64.encodeBase64String(bytes);
	        return encode;
	    	
	    	
	    }
	    public static String decryptDES(String encryptDES) throws Exception{
	    	String key="mini.com";
	 	    String transformation="DES";
	 	    String algorithm="DES";
	        Cipher cipher = Cipher.getInstance(transformation);
	        SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(),algorithm);
	        //Cipher.DECRYPT_MODE:表示解密
	        // 解密规则
	        cipher.init(Cipher.DECRYPT_MODE,secretKeySpec);
	        // 解密，传入密文
	        byte[] bytes = cipher.doFinal(Base64.decodeBase64(encryptDES));

	        return new String(bytes);
	}
}
